package com.ulic.customerManApplication.util.xml;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.util.config.ConfigInfo;

public class Dom4j {
	/**
	 * 得到Doc
	 * @return
	 */
	public static Document getDocment(){
		String encoding = ConfigInfo.getPropertiesValue(CONSTANT.XMLEncodeing);
		Document doc = DocumentHelper.createDocument();
		doc.setXMLEncoding(encoding);
		return doc;
	}
	
	/**
	 * 根据xpath得到对应的Element文本值
	 * @param para
	 * @param xpath
	 * @return
	 */
	public static String getText(String para,String xpath){
		Element ele = getElement(para, xpath);
		if(ele==null){
			return "";
		}
		return ele.getText();
	}
	
	
	/**
	 * 根据xpath得到对应的Element
	 * @param para
	 * @param xpath
	 * @return
	 */
	public static Element getElement(String para,String xpath){
		Document doc = getDocument(para);
		return (Element)doc.selectSingleNode(xpath);
	}
	
	/**
	 * 根据xpath得到某个对象
	 * @param doc
	 * @param xpath
	 * @return
	 */
	public static Element getElement(Document doc,String xpath){
		Element ele = (Element)doc.selectSingleNode(xpath);;
		return ele;
	}
	/**
	 * 根据xpath得到对应的xml数据对象
	 * @param para
	 * @param xpath
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Node> getElementsByPath(String para,String xpath){
		Document doc = getDocument(para);
		return doc.selectNodes(xpath);
	}
	
	/**
	 * 将xml形式字符串转换为标准的Document
	 * @param para
	 * @return
	 */
	private static Document getDocument(String para){
		Document doc = null;
		try {
		 doc =  DocumentHelper.parseText(para);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	public static void main(String args[]){
		//Document doc = getDocment();
		//doc.addElement("String");
		String str = "<?xml version='1.0' encoding='GBK'?><TransData><ClientInfo><ClientType>123</ClientType><Date>2008-03-31</Date><Time>11:30:30</Time><SeqNo>0000000000000000000001</SeqNo><Operator>CC001</Operator><BusinessCode>04010001</BusinessCode><SubBusinessCode>1</SubBusinessCode><TurnPageFlag>0</TurnPageFlag><OrderByFlag>0</OrderByFlag><OrderByField></OrderByField><RowNumStart>1</RowNumStart><PageRowNum>1000</PageRowNum><TotalRowNum></TotalRowNum><ResultCode>0</ResultCode><Result></Result></ClientInfo><InputData><ContNo>2010000000054088</ContNo><AppntName>中国邮政</AppntName><AppntIDType>0</AppntIDType><AppntIDNo>411122790223401</AppntIDNo></InputData><OutputData><Result><Name>test</Name></Result><Result><Name>test2</Name></Result></OutputData></TransData>";
		Document doc = getDocument(str);
		Element ele = Dom4j.getElement(doc, "/TransData/ClientInfo/ClientType");
		ele.setText("XXXXXXXXXXXX");
		System.out.println(doc.asXML());
		List<Node> lt = Dom4j.getElementsByPath(str, "/TransData/OutputData/*");
		for(Node node:lt){
			System.out.println(lt.size());
			System.out.println("name:"+node.getName()+" value="+node.getText());
		}
		System.out.println(Dom4j.getDocment());
		//System.out.println(Dom4j.getText(str, "/TransData/OutputData/Result[1]/Name[1]"));
		 } 
	

}
